﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using Web.SystemConfig.Services;
using System.Data;
using EPlanet.Web.UI.WebControls;

using DreamBuilder.UI.UserControls;
using DreamBuilder.Core.Message;
using CommonModule;
using DreamBuilder.Core.Container;
using DreamBuilder.Core.Log;
using Web.SystemLogin.Business;
using System.Threading;


namespace Web.Views.SystemConfig
{
    public partial class Auth_Role : PageBase
    {
        #region 变量定义
        /// <summary>
        /// Business类
        /// </summary>
        [Dependency]
        public IAuth_Role BUSAuth_Role
        {
            get;
            set;
        }
        #endregion

        #region PageLoad

        protected void Page_Load(object sender, System.EventArgs e)
        {try{
            SetPageProperty();
            InitControlsCultureInfo();
            if (!IsPostBack)
            {
                ShowList();
            }
            CreateToolMenu(Toolbar1, ToolMenuType.TYPE_MainList);
            base.CheckListAuthorities(Toolbar1, uclist);
        }

        catch (Exception ex)
        {
            WriteErrorLog(ex.Message);
            ExceptionMsg();
        }
        }
        /// <summary>
        /// 设置页面的菜单属性
        /// </summary>
        private void SetPageProperty()
        {
            this.DBHelpName = DBHelperString.Dictionary;
            base.MenuID = "ePlanet.Configuration.Auth_Role";
        }

        /// <summary>
        /// 显示列表,为列表控件的单据id赋值
        /// </summary>
        private void ShowList()
        {
            uclist.VoucherID = "vch_Auth_Role";
        }

        /// <summary>
        /// 初始化控件多语信息
        /// </summary>
        private void InitControlsCultureInfo()
        {
        }

        #endregion

        #region 新增

        /// <summary>
        /// 新增数据
        /// </summary>
        private void AddNewData()
        {
            RedirectEdit(string.Empty);
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除单条记录
        /// </summary>
        /// <param name="pkValue"></param>
        /// <returns></returns>
        private bool DeleteData(string pkValue)
        {
            bool deleteResult = false;
            ResultInfo result = new ResultInfo();
            if (!string.IsNullOrEmpty(pkValue))
            {
                DataSet dsSave = uclist.GetDeleteDSData(pkValue);
                DataSet dsDeleteChildInfo = BUSAuth_Role.GetDeleteChildInfo(pkValue);
                if (dsSave != null && dsDeleteChildInfo != null)
                {
                    foreach (DataTable dtDeleteChildInfo in dsDeleteChildInfo.Tables)
                    {
                        DataTable dtDeleteChildInfoCopy = dtDeleteChildInfo.Copy();
                        dtDeleteChildInfoCopy.TableName = dtDeleteChildInfo.TableName;
                        dsSave.Tables.Add(dtDeleteChildInfoCopy);
                    }
                }
                if (dsSave != null)
                {
                    string pkName = BUSAuth_Role.GetpkNameByRolepkValue(pkValue);
                    result = BUSAuth_Role.Delete(dsSave);
                    if (result.bSuccess == true)
                    {
                        string operationMessage = this.CurrentUser.Real_Name + "删除了" + pkName + "[" + pkValue + "]角色";
                        WriteUserLog(operationMessage,OracleLogType.System);
                        deleteResult = true;
                    }
                    else
                    {
                        DisplayMessage(result.ErrorDesc);
                    }
                }
            }
            else
            {
                result.bSuccess = false;
                result.ErrorDesc = "ERRDAE07";
                DisplayMessage(result.ErrorDesc);
            }
            return deleteResult;
        }

        /// <summary>
        /// 删除列表选中的多条记录
        /// </summary>
        private void DeleteSelectData()
        {
            string pkValues = uclist.GetListCheckedPrimaryValues(); 
            foreach (string pkValue in System.Text.RegularExpressions.Regex.Split(pkValues, ","))
            {
                DeleteData(pkValue);
            }
        }

        #endregion

        #region 事件处理方法
        /// <summary>
        /// 工具条事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Toolbar1_MenuItemClick(object sender, MenuItemClickEventArgs e)
        {try{
            switch (e.EventItem.ID.ToString().ToLower())
            {
                case "tbadd":
                    AddNewData();
                    break;
                case "tbdelete":
                    DeleteSelectData();
                    this.uclist.RefreshList();
                    break;
                case "tbexport":
                    this.ExportListToExcel(uclist.GetExportData(), this);
                    break;
                case "tbhelp":
                    break;
            }
        }
        catch (Exception ex)
        {
            WriteErrorLog(ex.Message);
            ExceptionMsg();
        }
        }

        /// <summary>
        /// 列表事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uclistEmployee_VoucherListRowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow gridViewRow = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            if (gridViewRow == null) return;
            int rowIndex = gridViewRow.RowIndex;
            GridView epGridView = sender as GridView;
            if (epGridView == null) return;
            string pkValue = epGridView.DataKeys[rowIndex][0].ToString().Trim();
            switch (e.CommandName.Trim().ToLower())
            {
                case "edit":
                    RedirectEdit(pkValue);
                    break;
                case "delete":
                    {
                        if (DeleteData(pkValue) == true)
                        {
                            this.uclist.RefreshList();
                        }
                    }
                    break;
                case "view":
                    {
                        ShowViewInfo(uclist.VoucherID, pkValue);
                    }
                    break;
            }
        }
        #endregion

        #region 页面跳转

        /// <summary>
        /// 跳转到编辑页面
        /// </summary>
        /// <param name="pkValue">主键值</param>
        private void RedirectEdit(string pkValue)
        {
            Response.Redirect("Auth_RoleEdit.aspx?pkValue=" + pkValue);
        }

        #endregion

        #region OnInit
        override protected void OnInit(EventArgs e)
        {
            //
            //CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        /// <summary>
        /// 添加页面及控件事件
        /// </summary>
        private void InitializeComponent()
        {
            this.Toolbar1.MenuItemClick += new EPlanet.Web.UI.WebControls.MenuItemClickedEventHandler(Toolbar1_MenuItemClick);
            this.uclist.VoucherListRowCommand += new global::DreamBuilder.UI.UserControls.VoucherListRowCommandEventHandler(uclistEmployee_VoucherListRowCommand);
        }
        #endregion
    }
}
